﻿package com.crl.module.chat.channel
{
	import com.crl.module.chat.model.MessageVO;
	import com.crl.utils.StringUtil;
	
	import flash.utils.Dictionary;
	import flash.utils.getTimer;
	
	
	/**
	 * 抽像通道;
	 * @author crl
	 * 
	 */	
	public class AbstractChannel implements IChannel
	{
		public static const LINK_EVENT:String="linkEvent";
		
		protected var type:int=0;
		protected var name:String;
		
		public var channelColor:Number;
		public var fromColor:Number;
		
		//只在私有通道里有;
		//public var toColor:Number;
		
		public var messageColor:Number;
		
		/**
		 * 最后发表时间; 
		 */		
		protected var lastSendTime:int;
		protected var timeLimit:int;
		
		
		protected var tips:Dictionary;
		protected var templete:String="[{0}]";
		protected var args:Array;
		

		public static function getFromHTML(vo:MessageVO):String{
			var html:String//=StringUtil.createLinkTag(LINK_EVENT,chatText.fromName,chatText.fromName,chatText.fromGUID);
			html//=StringUtil.createColorHtml("<u>"+html+"</u>",chatText.fromColor.toString(16));
			return html;
		}
		public static function getTOHTML(vo:MessageVO):String{
			var html:String//=StringUtil.createLinkTag(LINK_EVENT,chatText.toName,chatText.toName,chatText.toGUID);
			html//=StringUtil.createColorHtml("<u>"+html+"</u>",chatText.fromColor.toString(16));
			return html;
		}
		
		
		//public static function escChars
		
		
		public function AbstractChannel(name:String,color:int,timeLimit:int=1000)
		{
			this.name=name;
			this.channelColor=color;
			
			this.fromColor=0x66CCFF;
			//this.toColor=0x66CCFF;
			
			this.timeLimit=timeLimit;
			
			
			tips=new Dictionary(true);
			
			tips["timeLimit"]="抱歉，您说得太快了，慢点来！";
			
			args=[];
			
			
		}
		

		public function getType():int{
			return type;
		}
		
		
		/**
		 * 外部调用渲染方法; 
		 * @param chatText
		 * @return 
		 * 
		 */		
		public function parseToString(vo:MessageVO):String
		{
			return getPrefix()+parseMessage(vo);
		}
	
		
		/**
		 * 子类实现功能; 
		 * @param chatText
		 * @return 
		 * 
		 */		
		protected function parseMessage(vo:MessageVO):String{
			var message:String=vo.m;
			if(vo.fk){
				message=message.replace(/\{fn\}/g,AbstractChannel.getFromHTML(vo));
			}
			if(vo.tk){
				message=message.replace(/\{tn\}/g,AbstractChannel.getTOHTML(vo));
			}
			
			
			return message;
		}
		
		public function getPrefix():String{

			var html:String=StringUtil.createColorHtml(name,channelColor.toString(16));
			return html;
		}
		
		
		public function check():void{
			var time:int = getTimer();
			if(time - lastSendTime < timeLimit){
				throw new Error(getTipByKey("timeLimit"));
			}
			lastSendTime = time;
		}
		
		public function getTipByKey(key:String):String{
			return tips[key];
		}
		
		public function isClosed():Boolean
		{
			return false;
			
		}
		
		public function getName():String
		{
			return this.name;
		}
		
	}
}